<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { Close } from '@element-plus/icons-vue'

defineProps({
  show: Boolean
})

defineEmits(['close'])

const currentView = ref('login'); // login | forgot | register

const switchView = (view) => {
  currentView.value = view;
}

const forms = reactive({
  login: {
    username: '',
    password: ''
  },
  forgot: {
    email: '',
    captcha: '',
    newPassword: ''
  },
  register: {
    email: '',
    captcha: '',
    password: '',
    confirmPassword: ''
  }
})

// 倒计时相关状态
const countdown = ref(0);
let timer = null;

const sendCaptcha = () => {
  if (!(forms.forgot.email || forms.register.email)) {
    ElMessage.warning('请输入邮箱地址');
    return;
  }

  if (countdown.value > 0) return;

  // 模拟发送验证码
  ElMessage.success(`验证码已发送至 ${forms.forgot.email || forms.register.email}`);

  // 开始倒计时
  countdown.value = 60;
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
    }
  }, 1000);
};

const handleSubmit = () => {
  switch(currentView.value) {
    case 'login':
      ElMessage.success('登录提交: ' + forms.login.username);
      break;
    case 'forgot':
      ElMessage.success('已发送重置邮件至: ' + forms.forgot.email);
      switchView('login');
      break;
    case 'register':
      ElMessage.success('注册提交: ' + forms.register.email);
      switchView('login');
      break;
  }
}
</script>

<template>
  <Transition name="modal">
    <div v-if="show" class="modal-mask">
      <div class="login-card">
        <div class="close-btn" @click="$emit('close')">
          <el-icon><Close /></el-icon>
        </div>

        <div class="content-container">
          <!-- 登录视图 -->
          <template v-if="currentView === 'login'">
            <div class="logo-container">
              <img src="@/assets/logo.png" alt="校徽" class="logo">
              <h1>西南石油大学人事系统</h1>
            </div>

            <form @submit.prevent="handleSubmit">
              <div class="form-group">
                <label>工号/邮箱</label>
                <el-input v-model="forms.login.username" type="text" required/>
              </div>
              <div class="form-group">
                <label>密码</label>
                <el-input type="password" v-model="forms.login.password" required/>
              </div>

              <button type="submit" class="login-btn">登 录</button>

              <div class="extra-links">
                <a @click.prevent="switchView('forgot')">忘记密码？</a>
                <a @click.prevent="switchView('register')">新用户注册</a>
              </div>
            </form>
          </template>

          <!-- 找回密码视图 -->
          <template v-else-if="currentView === 'forgot'">
            <div class="header">
              <h2>找回密码</h2>
              <p>请输入注册邮箱接收重置验证码</p>
            </div>

            <form @submit.prevent="handleSubmit">
              <div class="form-group">
                <label>注册邮箱</label>
                <el-input type="text" v-model="forms.forgot.email" required/>
              </div>
              <div class="form-group">
                <label>邮箱验证码</label>
                <el-input v-model="forms.forgot.captcha" type="text" required>
                  <template #suffix>
                    <el-button :disabled="countdown > 0" @click="sendCaptcha" class="captcha-btn">
                      {{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
                    </el-button>
                  </template>
                </el-input>
              </div>
              <div class="form-group">
                <label>新密码</label>
                <el-input type="password" v-model="forms.forgot.newPassword" required/>
              </div>

              <button type="submit" class="login-btn">重置密码</button>

              <div class="extra-links">
                <a @click.prevent="switchView('login')">返回登录</a>
              </div>
            </form>
          </template>

          <!-- 注册视图 -->
          <template v-else>
            <div class="header">
              <h2>新用户注册</h2>
              <p>请输入注册信息</p>
            </div>

            <form @submit.prevent="handleSubmit">
              <div class="form-group">
                <label>邮箱</label>
                <el-input type="text" v-model="forms.register.email" required/>
              </div>
              <div class="form-group">
                <label>邮箱验证码</label>
                <el-input v-model="forms.register.captcha" type="text" required>
                  <template #suffix>
                    <el-button :disabled="countdown > 0" @click="sendCaptcha" class="captcha-btn">
                      {{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
                    </el-button>
                  </template>
                </el-input>
              </div>
              <div class="form-group">
                <label>密码</label>
                <el-input type="password" v-model="forms.register.password" required/>
              </div>
              <div class="form-group">
                <label>确认密码</label>
                <el-input type="password" v-model="forms.register.confirmPassword" required/>
              </div>

              <button type="submit" class="login-btn">立即注册</button>

              <div class="extra-links">
                <a @click.prevent="switchView('login')">已有账号？立即登录</a>
              </div>
            </form>
          </template>
        </div>
      </div>
    </div>
  </Transition>
</template>

<style scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
}

.login-card {
  background: white;
  padding: 2rem;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  cursor: pointer;
  color: #999;
  transition: color 0.2s;
  z-index: 1;
}

.close-btn:hover {
  color: #666;
}

.close-btn svg {
  display: block;
}

.logo-container {
  text-align: center;
  margin-bottom: 2rem;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 1rem;
}

h1 {
  color: #002A5C;
  font-size: 1.5rem;
  margin: 0;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  color: #002A5C;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #ADD8E6;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: background 0.3s ease;
}

.login-btn:hover {
  background: #003366;
}

.extra-links {
  margin-top: 1.5rem;
  display: flex;
  justify-content: space-between;
}

a {
  color: #002A5C;
  text-decoration: none;
  font-size: 0.9rem;
  cursor: pointer;
}

a:hover {
  text-decoration: underline;
}

.content-container {
  transition: all 0.3s ease;
}

.header {
  text-align: center;
  margin-bottom: 2rem;
}

.header h2 {
  color: #002A5C;
  margin-bottom: 0.5rem;
}

.header p {
  color: #666;
  font-size: 0.9rem;
}

.captcha-btn {
  transform: translateX(10px);
}

/*@media (max-width: 480px) {
  .login-card {
    padding: 1.5rem;
    margin: 0 15px;
  }

  h1 {
    font-size: 1.3rem;
  }

  input {
    padding: 10px;
  }

  .login-btn {
    padding: 12px;
  }
}*/

/* 过渡动画 */
.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}

.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}

.view-enter-active,
.view-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}
.view-enter-from {
  opacity: 0;
  transform: translateX(20px);
}
.view-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}
</style>